<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :okButtonProps="{ props: {disabled: disableSubmit} }"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="禁止下单id"
          :hidden="hiding"
          hasFeedback>
          <a-input placeholder="请输入禁止下单id" v-decorator="['blackId', {'initialValue':this.mainId}]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="省份/城市/地区">
          <a-cascader :options="areaData" :value="areaValue" change-on-select  @change="onChange" placeholder="Please select" />
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import {httpAction, getAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from 'moment'
  import JDate from '@/components/jeecg/JDate'

  export default {
    components: {
      JDate
    },
    name: 'OmsOrderBlackAreaModal',
    data() {
      return {
        title: '操作',
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5}
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16}
        },
        moment,
        areaData: [],
        areaValue:[],
        format: 'YYYY-MM-DD HH:mm:ss',
        disableSubmit: false,
        mainId: '',
        hiding: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
        },
        url: {
          add: '/oms/omsOrderBlack/addOmsOrderBlackArea',
          edit: '/oms/omsOrderBlack/editOmsOrderBlackArea',
          areaJson: "/data/area",
        }
      }
    },
    created() {
    },
    methods: {
      add(mainId) {
        if (mainId) {
          this.edit({mainId}, '')
        } else {
          this.$message.warning('请选择一条数据')
        }
      },
      detail(record) {
        this.edit(record, 'd')
      },
      edit(record, v) {
        if (v == 'e') {
          this.hiding = false;
          this.disableSubmit = false;
        } else if (v == 'd') {
          this.hiding = false;
          this.disableSubmit = true;
        } else {
          this.hiding = true;
          this.disableSubmit = false;
        }
        this.getArea();
        this.form.resetFields();
        this.mainId = record.mainId;
        this.model = Object.assign({}, record);
        this.areaValue = [this.model.provinceCode,this.model.cityCode,this.model.areaCode];
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'blackId' ))
          // 时间格式化
        })
      },
      getArea() {
        getAction(this.url.areaJson).then((res) => {
          this.areaData = res;
        })
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            if ( formData.provinceCode === null  || formData.provinceCode === '' || formData.provinceCode === undefined ) {
              that.confirmLoading = false;
              this.$message.warning('省份/城市/地区不可为空！');
              return;
            }

            //时间格式化
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel() {
        this.close();
      },
      onChange(value, selectedOptions) {
        console.log(value, selectedOptions);
        switch (value.length) {
          case 0:
            this.model.provinceCode = '';
            this.model.province = '';
            this.model.cityCode = '';
            this.model.city = '';
            this.model.areaCode = '';
            this.model.area = '';
            break
          case 1:
            this.model.provinceCode = selectedOptions[0].value;
            this.model.province = selectedOptions[0].label;
            this.model.cityCode = '';
            this.model.city = '';
            this.model.areaCode = '';
            this.model.area = '';
            break
          case 2:
            this.model.provinceCode = selectedOptions[0].value;
            this.model.province = selectedOptions[0].label;
            this.model.cityCode = selectedOptions[1].value;
            this.model.city = selectedOptions[1].label;
            this.model.areaCode = '';
            this.model.area = '';
            break
          case 3:
            this.model.provinceCode = selectedOptions[0].value;
            this.model.province = selectedOptions[0].label;
            this.model.cityCode = selectedOptions[1].value;
            this.model.city = selectedOptions[1].label;
            this.model.areaCode = selectedOptions[2].value;
            this.model.area = selectedOptions[2].label;
            break
          default:
            break
        }
        this.model.level = value.length;
        this.areaValue = [this.model.provinceCode,this.model.cityCode,this.model.areaCode];
      },
    }
  }
</script>

<style scoped>

</style>
